package com.ofa.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.ofa.base.Pagination;
import com.ofa.entity.Module;
import com.ofa.entity.vo.ModuleVO;
import com.ofa.service.ModuleService;
import com.ofa.util.CommonUtils;
import com.opensymphony.xwork2.ActionSupport;

public class ModuleAction extends ActionSupport {
	
	private int maxResults = 10;
	private int firstResult = 0;
	
	private int id;
	private List<Serializable> ids;

	private ModuleService moduleService;
	
	private Module module;
	
	private List<Module> moduleList;
	
	private Pagination<Module> pagination;
	
	private String jsonString;
	
	
	@Override
	public String execute(){
		String offset = ServletActionContext.getRequest().getParameter("pager.offset");
		if(CommonUtils.isNotEmpty(offset))
			firstResult = Integer.valueOf(offset);
		pagination = moduleService.queryModules(module, firstResult, maxResults);
		return SUCCESS;
	}
	
	public String simpleTree(){
		moduleList = moduleService.queryModuleTree();
		return "simpleTree";
	}
	
	public String standardTree(){
		List<ModuleVO> moduleVOList = new ArrayList<ModuleVO>();
		Module module = null;
		moduleList = moduleService.queryModuleTree();
		for(int i=0; i<moduleList.size(); i++){
			module = moduleList.get(i);
			ModuleVO moduleVO = new ModuleVO();
			moduleVO.setId(module.getId());
			if(module.getParent() != null)
				moduleVO.setpId(module.getParent().getId());
			moduleVO.setName(module.getName());
			moduleVO.setSortNo(module.getSortNo());
			moduleVO.setOpen(true);
			moduleVOList.add(moduleVO);
		}
		//jsonString = JSONArray.fromObject(moduleVOList).toString();  
		if(jsonString == null)
			jsonString = "{}";
		return "standardTree";
	}
	
	public String leftModule(){
		moduleList = moduleService.queryModules(module, 0);
		return "leftModule";
	}
	
	public String toSave(){
		moduleList = moduleService.queryModules(module, 0);
		return "toSave";
	}
	
	public String save(){
		if(module.getParent().getId() == null)
			module.setParent(null);
		moduleService.save(module);
		return "save";
	}

	public String toUpdate(){
		module = moduleService.get(id);
		moduleList = moduleService.queryModules(module, 0);
		return "toUpdate";
	}
	
	public String update(){
		moduleService.update(module);
		return "update";
	}
	
	public String delete(){		
		moduleService.delete(ids);
		return "delete";
	}
	
	public String get(){
		module = moduleService.get(id);
		return "get";
	}

	
	
	public ModuleService getModuleService() {
		return moduleService;
	}
	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public Module getModule() {
		return module;
	}
	public void setModule(Module module) {
		this.module = module;
	}


	public List<Module> getModuleList() {
		return moduleList;
	}
	public void setModuleList(List<Module> moduleList) {
		this.moduleList = moduleList;
	}

	public Pagination<Module> getPagination() {
		return pagination;
	}
	public void setPagination(Pagination<Module> pagination) {
		this.pagination = pagination;
	}

	public int getMaxResults() {
		return maxResults;
	}
	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}

	public int getFirstResult() {
		return firstResult;
	}
	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public List<Serializable> getIds() {
		return ids;
	}
	public void setIds(List<Serializable> ids) {
		this.ids = ids;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	
	
}
